#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：api_automation_test 
@File    ：send_email.py
@IDE     ：PyCharm 
@Author  ：XiaoDengYa
@Date    ：2021/7/22/022 11:47 
"""

import os
import zipfile

import yagmail

from common.tools import logger


class EmailServe:

    @staticmethod
    def zip_report(file_path: str, out_path: str):
        """
        压缩指定文件夹
        :param file_path: 目标文件夹路径
        :param out_path: 压缩文件保存路径+xxxx.zip
        :return: 无
        """

        zip = zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED)
        for path, dirnames, filenames in os.walk(file_path):
            # 去掉目标跟路径，只对目标文件夹下边的文件及文件夹进行压缩
            fpath = path.replace(file_path, '')

            for filename in filenames:
                zip.write(
                    os.path.join(
                        path, filename), os.path.join(
                        fpath, filename))
        zip.close()

    @staticmethod
    def send_email(setting: dict, enclosures: str):
        """
        入参一个字典
        :param setting: 传递dict信息集合
        :param user: 发件人邮箱
        :param password: 邮箱授权码
        :param host: 发件人使用的邮箱服务 例如：smtp.163.com
        :param contents: 内容
        :param addressees: 收件人列表
        :param title: 邮件标题
        :param enclosures: 附件列表
        :param enclosures: 需要发送的压缩包
        :return:
        """
        yag = yagmail.SMTP(
            setting['user'],
            setting['password'],
            setting['host'])
        # 发送邮件
        yag.send(
            setting['addressees'],
            setting['title'],
            setting['contents'],
            enclosures)
        # 关闭服务
        yag.close()
        logger.info("邮件发送成功！")


if __name__ == '__main__':
    report_path = r"F:\script\StudyReview\automation\api_automation_test\output\2021-07-22_15_05"
    EmailServe().zip_report(report_path, "report.zip")
